#include "AdbScreen.h"
#include <QProcess>
#include <QDebug>

AdbScreen::AdbScreen(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);
    bStart = false;
    mScale = 0.3f;
    mWakeTime = 5000; //ms
}


void AdbScreen::paintEvent(QPaintEvent *event){
	QPainter painter(this);
    painter.drawImage(QRect(0, 0, mImage.width()*mScale, mImage.height()*mScale), mImage);
}

void AdbScreen::resizeEvent(QResizeEvent *event){

}
void AdbScreen::mousePressEvent(QMouseEvent *event){

}
void AdbScreen::mouseReleaseEvent(QMouseEvent *event){
    int x = event->x();
    int y = event->y();
    command(QString("D:/Android/sdk/platform-tools/adb.exe shell input tap %1 %2").arg(x / mScale).arg(y / mScale));
}
void AdbScreen::mouseDoubleClickEvent(QMouseEvent *event){

}
void AdbScreen::mouseMoveEvent(QMouseEvent *event){

}
void AdbScreen::keyPressEvent(QKeyEvent *event){
    int key = event->key();
    if (Qt::Key_F5 == key){
        command("D:/Android/Sdk/platform-tools/adb.exe shell screencap -p /sdcard/screencap.png");
        command("D:/Android/sdk/platform-tools/adb.exe pull /sdcard/screencap.png screencap.png");
        mImage.load("screencap.png");
        setFixedSize(mImage.width()*mScale, mImage.height()*mScale);
        repaint();
    }
    else{

    }
}
void AdbScreen::keyReleaseEvent(QKeyEvent *event){

}
void AdbScreen::command(QString action){
	QProcess p;
    qDebug() << "command:" << action;
	p.start(action);
	if (!p.waitForFinished()){
		qDebug() << "QProcess run error!";
	}
}

void AdbScreen::on_actionStart_triggered()
{
    if (bStart){
        ui.actionStart->setText("stop");
        bStart = false;
    }
    else{
        ui.actionStart->setText("start");
        bStart = true;
    }
}

void AdbScreen::on_action20_triggered()
{
    mScale = 0.2f;
    repaint();
}

void AdbScreen::on_action30_triggered()
{
    mScale = 0.3f;
    repaint();
}

void AdbScreen::on_action50_triggered()
{
    mScale = 0.5f;
    repaint();
}

void AdbScreen::on_action100_triggered()
{
    mScale = 1.0f;
    repaint();
}

void AdbScreen::on_actionHorizontal_triggered()
{

}

void AdbScreen::on_actionVertical_triggered()
{

}

